package com.huichenghe.xinlvsh01.expand_activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.SyncStateContract;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.huichenghe.xinlvsh01.Adapter.MYRecyclerAdapter;
import com.huichenghe.xinlvsh01.R;
import com.tencent.connect.dataprovider.Constants;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class ShareActivity extends AppCompatActivity
{
    private RecyclerView shareIcon;
    private ArrayList<Integer> data;
    private MyShareRecyclerAdapter adapter;
    private AppCompatImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        settingTheStatebarAndNavigationbar();
        initToolbar();
        initOther();

    }

    private void initOther()
    {

        mImageView = (AppCompatImageView)findViewById(R.id.image_shot_screen);

        File file = new File("/sdcard/抽样.png");
        if(file.exists())
        {
            try {
                FileInputStream inputStream = new FileInputStream(file);
                Bitmap b = BitmapFactory.decodeStream(inputStream);
                mImageView.setImageBitmap(b);
                inputStream.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }

        adapter = new MyShareRecyclerAdapter();
        data = new ArrayList<Integer>();
        data.add(R.drawable.qq_true);
        data.add(R.drawable.room_true);
        data.add(R.drawable.wechart_true);
        data.add(R.drawable.frendly_true);
        data.add(R.drawable.weibo_true);
        data.add(R.drawable.renren_true);
        shareIcon = (RecyclerView)findViewById(R.id.share_icon);
        shareIcon.setItemAnimator(new DefaultItemAnimator());
        LinearLayoutManager manager = new LinearLayoutManager(ShareActivity.this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        shareIcon.setLayoutManager(manager);
        shareIcon.setHasFixedSize(true);
        shareIcon.setAdapter(adapter);

    }


    private void initToolbar()
    {
        Toolbar toolbar = (Toolbar)findViewById(R.id.share_toolbar);
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(myMenuListener);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareActivity.this.onBackPressed();
            }
        });

    }

    /**
     * 设置沉浸式状态栏和底部虚拟键全屏
     */
    private void settingTheStatebarAndNavigationbar() {
//        // 透明状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //底部虚拟键透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }


    Toolbar.OnMenuItemClickListener myMenuListener = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId())
            {
                case R.id.share_menu_send:
                    Toast.makeText(ShareActivity.this, "share to send", Toast.LENGTH_SHORT).show();


                    break;
            }
            return true;
        }
    };

    private void sendToTarget()
    {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
        intent.putExtra(Intent.EXTRA_TEXT, "");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, getTitle()));




    }

    /**
     * 创建菜单
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_share, menu);
        return true;
    }



    class MyShareRecyclerAdapter extends RecyclerView.Adapter<MyShareRecyclerAdapter.MyHolder>
    {
        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View v = LayoutInflater.from(ShareActivity.this).inflate(R.layout.item_for_share_screen, parent, false);
            return new MyHolder(v);
        }

        @Override
        public void onBindViewHolder(MyHolder holder, final int position)
        {
            holder.checkBox.setBackgroundResource(data.get(position));
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
//                    Toast.makeText(ShareActivity.this, "分享到。。。", Toast.LENGTH_SHORT).show();
                    toSendShare(position);

//                    sendToTarget();
                }
            });
        }

        private void toSendShare(int position)
        {
            switch (position)
            {
                case 0:

                    shareToQQ();


                break;
            }

        }

        private void shareToQQ()
        {
            Bundle bundle = new Bundle();
//            bundle.putString(SyncStateContract.Constants.);

        }


        @Override
        public int getItemCount()
        {
            return data.size();
        }



        class MyHolder extends RecyclerView.ViewHolder
        {
            CheckBox checkBox;
            public MyHolder(View itemView)
            {
                super(itemView);
                checkBox = (CheckBox)itemView.findViewById(R.id.share_target);
            }
        }
    }

}
